﻿$(function() {
    var a = false;
    var b = function() {
        var x = parseInt($("#hidShopMoney").val());
        var ffdk = parseInt($("#pointsbl").val());
        var fb = parseInt($("#pointfb").val());
        var d = $("#hidBalance").val();
        var t = parseInt($("#hidPoints").val());
        var tfb = parseInt($("#hidfb").val());
        var cb = $("#spFubi");
        var c = $("#spPoints");
        var p = $("#spBalance");
        var h = null;
        var m = $("#bankList");
        var shopnum = parseInt($("#shopnum").val());
        var r = "手机支付宝";
        //var g = parseInt(t / 100) > x ? x: parseInt(t / 100);
        var g = ffdk > x ? x: ffdk;
        var w = 0;
        var e = 0;
        var checkpay='no';//选择支付方式
        var checkpayfubi='no';//选择支付方式
        var checkpayfufen='no';//选择支付方式
        var banktype='nobank';

        if (g < x) {
            var j = parseInt(d);
            if (j > 0) {
                var i = x - g;
                if (j >= i) {
                    w = i
                } else {
                    w = j;
                    e = i - j
                }
            } else {
                e = x - g
            }
        }

        var q = function(y) {
            g = y;
            if (y > 0) {
                c.parent().removeClass("z-pay-grayC");
                c.attr("sel", "1").attr("class", "z-pay-mentsel").next("span").html("福分支付（您的福分：" + t + "）")
                // c.attr("sel", "1").attr("class", "z-pay-mentsel").next("span").html('福分支付<em class="orange">' + y + ".00</em>元（您的福分：" + t + "）")
                checkpayfufen='fufen';
                banktype='nobank';
            } else {
                c.parent().addClass("z-pay-grayC");
                checkpayfufen='no';
                c.attr("sel", "0").attr("class", "z-pay-ment").next("span").html('福分支付（您的福分：' + t + "）")
                // c.attr("sel", "0").attr("class", "z-pay-ment").next("span").html('福分支付<em class="orange">0.00</em>元（您的福分：' + t + "）")
            }
        };
        var qb = function(y) {
            if (y > 0) {
                cb.parent().removeClass("z-pay-grayC");
                cb.attr("sel", "1").attr("class", "z-pay-mentsel").next("span").html("福币支付（您的福币：" + tfb + "）")
                // cb.attr("sel", "1").attr("class", "z-pay-mentsel").next("span").html('福币支付<em class="orange">' + y + ".00</em>元（您的福币：" + tfb + "）")
                checkpayfubi='fubi';
                banktype='nobank';
            } else {
                cb.parent().addClass("z-pay-grayC");
                checkpayfubi='no';
                cb.attr("sel", "0").attr("class", "z-pay-ment").next("span").html('福币支付（您的福币：' + tfb + "）")
                // cb.attr("sel", "0").attr("class", "z-pay-ment").next("span").html('福币支付<em class="orange">0.00</em>元（您的福币：' + tfb + "）")
            }
        };
        var f = function(y) {
            w = y;
            if (y > 0) {
                p.parent().removeClass("z-pay-grayC");
                p.attr("sel", "1").attr("class", "z-pay-mentsel").next("span").html("余额支付（账户余额：" + d + " 元）")
                // p.attr("sel", "1").attr("class", "z-pay-mentsel").next("span").html('余额支付<em class="orange">' + y + ".00</em>元（账户余额：" + d + " 元）")
                checkpay='money';
                banktype='nobank';
            } else {
                checkpay='no';
                p.parent().addClass("z-pay-grayC");
                p.attr("sel", "0").attr("class", "z-pay-ment").next("span").html('余额支付（账户余额：' + d + " 元）")
                // p.attr("sel", "0").attr("class", "z-pay-ment").next("span").html('余额支付<em class="orange">0.00</em>元（账户余额：' + d + " 元）")
            }
        };
        var k = function(y) {
            e = y;
            if (y > 0) {
                h.html('<s class="z-arrow"></s>选择' + (r == "网银" ? "网银": '<b class="z-mlr">' + r + "</b>") + "支付剩余金额");
                // h.html('<s class="z-arrow"></s>选择' + (r == "网银" ? "网银": '<b class="z-mlr">' + r + "</b>") + "支付" + ((g > 0 || w > 0) ? "剩余": "") + '<em class="orange">' + e + ".00</em>元");
                h.removeClass("z-pay-grayC").nextAll().show();
                o = true
                checkpay='bank'
                if(r=='建设银行'){
                  banktype='CCB-WAP';
                }else if(r=='微信支付'){
                  banktype='wxpay_wap';
                }else if(r=='手机支付宝'){
                  banktype='wapalipay';
                }else if(r=='工商银行'){
                  banktype='ICBC-WAP';
                }
            } else {
                // h.addClass("z-pay-grayC").html('<s class="z-arrow"></s>选择支付方式支付').nextAll().hide();
                o = false
            }
        };


        if (ffdk > 0) {
            c.parent().click(function() {
                k(0);
                if (c.attr("sel") == 1) {
                    q(0);
                    // n(x)
                } else {
                    var y = ffdk;
                    if (y > 0) {
                        q(y >= x ? x: y);
                        // n(y >= x ? 0 : x - y)
                    } else {
                        // n(x)
                    }
                }
            });
            var n = function(z) {
                if (p.attr("sel") == 1) {
                    var y = parseInt(d) - z;
                    if (y > 0) {
                        f(z)
                    } else {
                        f(parseInt(d));
                        k( - y)
                    }
                } else {
                    k(z)
                }
            }
        }
        if (fb > 0) {
            cb.parent().click(function() {
                k(0);
                if (cb.attr("sel") == 1) {
                    qb(0);
                    // nfb(x)
                } else {
                    var y = fb;
                    if (y > 0) {
                        qb(y >= x ? x: y);
                        // nfb(y >= x ? 0 : x - y)
                    } else {
                        // nfb(x)
                    }
                }
            });
            var nfb = function(z) {
                if (p.attr("sel") == 1) {
                    var y = parseInt(d) - z;
                    if (y > 0) {
                        f(z)
                    } else {
                        f(parseInt(d));
                        k( - y)
                    }
                } else {
                    k(z)
                }
            }
        }
        if (parseInt(d) > 0) {
            p.parent().click(function() {
                k(0);
                if (p.attr("sel") == 1) {
                    f(0);
                    // l(x)
                } else {
                    var y = parseInt(d);
                    if (y > 0) {
                        f(y >= x ? x: y);
                        // l(y >= x ? 0 : x - y)
                    } else {
                        // l(x)
                    }
                }
            });
            var l = function(z) {
                if (c.attr("sel") == 1) {
                    var y = ffdk - z;
                    if (y > 0) {
                        q(z)
                    } else {
                        q(ffdk);
                        k( - y)
                    }
                } else {
                    k(z)
                }
            }
        }
        var o = false;
        var v = 1;
        $("li", m).each(function(y) {
            var z = $(this);

            if (y == 0) {
                h = z;
                h.click(function() {
                    if (!h.hasClass("z-pay-grayC")) {
                        if (!o) {
                            h.nextAll().show();
                            o = true
                        } else {
                            h.nextAll().hide();
                            o = false
                        }
                    }
                })
            } else {
                z.click(function() {
                    v = y;
                    r = z.text();
                    z.children("i").attr("class", "z-bank-Roundsel");
                    z.siblings().each(function() {
                        $(this).children("i").attr("class", "z-bank-Round")
                    });
                    h.html('<s class="z-arrow"></s>选择<b class="z-mlr">' + r + "</b>支付" + ((g > 0 || w > 0) ? "剩余金额": ""))
                    // h.html('<s class="z-arrow"></s>选择<b class="z-mlr">' + r + "</b>支付" + ((g > 0 || w > 0) ? "剩余": "") + '<em class="orange">' + e + ".00</em>元")
                    checkpay='bank'
                    //banktype=r;
                if(r=='建设银行'){
                  banktype='CCB-WAP';
                }else if(r=='微信支付'){
                  banktype='wxpay_wap';
                }else if(r=='手机支付宝'){
                  banktype='wapalipay';
                }else if(r=='工商银行'){
                  banktype='ICBC-WAP';
                }
                })
            }
        });
        if (e > 0) {
            h.removeClass("z-pay-grayC").html('<s class="z-arrow"></s>选择<b class="z-mlr">' + r + "</b>支付剩余金额").nextAll().show();
            // h.removeClass("z-pay-grayC").html('<s class="z-arrow"></s>选择<b class="z-mlr">' + r + "</b>支付" + ((g > 0 || w > 0) ? "剩余": "") + '<em class="orange">' + e + ".00</em>元").nextAll().show();
            o = true
            banktype='wxpay_wap';
            checkpay='bank'
        } else {
            h.addClass("z-pay-grayC").nextAll().hide();
            o = false
        }
        var s = $("#btnPay");
        var u = function() {
            var payment = $('.z-bank-Roundsel').length;
            if (payment) {
                banktype = $('.z-bank-Roundsel').parent('li').attr('urm');
            }

            var submitcode = Path.submitcode;
            if(!this.cc){
                this.cc = 1;
            }else{
                // alert("不可以重复提交订单!")
                // return false;
            }
            if(checkpayfufen=='no' && checkpayfubi=='no' && checkpay=='no' && banktype=='nobank'){
              alert("请选择一种支付方式！");
              if(this.cc){
                this.cc = false;
              }
              return
            }
            if (!a) {
                return
            }

            if (shopnum == 0) {
                location.href = Gobal.Webpath+"/mobile/cart/paysubmit/"+checkpay+"/"+banktype+"/"+x+"/"+t+"/"+submitcode+"/"+checkpayfufen+"/"+checkpayfubi+"/"
            } else {
                if (shopnum == 1) {
                    alert("亲，您的购物车中没有商品哦，去选购一些吧。");
                    location.replace(Gobal.Webpath+"/mobile/cart/cartlist")
                } else {
                    location.reload()
                }
            }

            // if (w + g >= x) {
            //     a = false;
            //     s.unbind("click").addClass("dis");
            //     if (shopnum != -1) {
            //         if (shopnum == 0) {
            //             location.replace(Gobal.Webpath+"/mobile/cart/paysubmit/"+checkpay+"/"+banktype+"/"+x+"/"+t+"/"+submitcode+"/"+checkpayfufen+"/"+checkpayfubi+"/")
            //         } else {
            //             if (shopnum == 1) {
            //                 alert("亲，您的购物车中没有商品哦，去选购一些吧。");
            //                 location.replace(Gobal.Webpath+"/mobile/cart/cartlist")
            //             } else {
            //                 if (shopnum == 10) {
            //                     location.reload()
            //                 }
            //             }
            //         }
            //     }
            //     s.bind("click", u).removeClass("dis");
            //     a = true
            // } else {
            //     if (e > 0 || checkpayfubi=='fubi' || checkpayfufen=='fufen') {
            //         if (v == 1 || v == 2 || v == 3) {
            //                location.href = Gobal.Webpath+"/mobile/cart/paysubmit/"+checkpay+"/"+banktype+"/"+x+"/"+t+"/"+submitcode+"/"+checkpayfufen+"/"+checkpayfubi+"/"
            //         }
            //     }
            // }
        };
        s.bind("click", u);
        a = true
    };
    Base.getScript(Gobal.Skin + "/js/mobile/pageDialog.js", b)
});